package com.pcbly.web.persistence.dict;

import java.util.List;

import org.hibernate.Session;

import com.pcbly.web.bean.dict.DictCategoryAddChildBean;
import com.pcbly.web.bean.dict.DictCategoryBean;
import com.pcbly.web.bean.dict.DictCategoryDependBean;
import com.pcbly.web.components.utils.random.GUID;
import com.pcbly.web.persistence.HibernateBaseDao;

@SuppressWarnings("unchecked")
public class DictCategoryHibernateDao extends
		HibernateBaseDao<DictCategoryBean> implements DictCategoryDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1168507614838327399L;

	protected Class<DictCategoryBean> getEntityClass() {
		return DictCategoryBean.class;
	}

	public DictCategoryHibernateDao() {
		super();
	}

	@Override
	public List<DictCategoryAddChildBean> queryAllDictCategoryAddChild()
			throws Exception {
		// TODO Auto-generated method stub
//		StringBuilder sb = new StringBuilder();
//		sb.append("select distinct a.ID,a.NAME_,a.USERDEFINED ");
//		sb.append(",a.ORDERBY_,a.DESC_, COUNT(b.ID) CHILD_ ");
//		sb
//				.append(" from sys_dict a left join sys_dict_comb b on a.ID=b.DICTID  ");
//		sb.append(" group by a.ID,a.NAME_,a.USERDEFINED,a.ORDERBY_,a.DESC_");
//		sb.append(" ORDER BY a.ORDERBY_ ");
		List list = getSession().createQuery("from DictCategoryAddChildBean order by orderBy ").list();
		// Iterator it=list.iterator();
		// while(it.hasNext()){
		// System.out.println(it.next());
		// }
		return list;
	}

	@Override
	public DictCategoryBean findById(String id) throws Exception {
		// TODO Auto-generated method stub
		List list = getHibernateTemplate().find(
				" from DictCategoryBean where id=? ", new Object[] { id });
		if (list.isEmpty()) {
			return null;
		} else {
			return (DictCategoryBean) list.get(0);
		}
	}

	@Override
	public void createDictCategoryBean(DictCategoryBean bean) throws Exception {
		// TODO Auto-generated method stub
		Session session = getSession();
		try {
			session.save(bean);
			String childId = bean.getChildDictId();
			if (!(childId == null || "".equals(childId.trim()))) {
				String[] arrayId = childId.split("::");
				for (int i = 0; i < arrayId.length - 1; i++) {
					DictCategoryDependBean depend = new DictCategoryDependBean();
					depend.setChildId(arrayId[i]);
					depend.setMasterId(bean.getId());
					GUID guid = new GUID();
					depend.setId(guid.toString());
					session.save(depend);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void updateDictCategoryBean(DictCategoryBean bean) throws Exception {
		// TODO Auto-generated method stub
		Session session = getSession();
		try {
			session.update(bean);
			String childId = bean.getChildDictId();
			// DictCategoryDependBean depend = new DictCategoryDependBean();
			// depend.setMasterId(bean.getId());
			session.createQuery(
					"delete from DictCategoryDependBean c where c.masterId=? ")
					.setString(0, bean.getId()).executeUpdate();
			// session.delete("from DictCategoryDependBean c where
			// c.masterId='"+bean.getId()+"' ");
			System.out.println(bean.getId());
			// logger.debug(depend.toString());
			if (!(childId == null || "".equals(childId.trim()))) {
				String[] arrayId = childId.split("::");
				for (int i = 0; i < arrayId.length - 1; i++) {
					DictCategoryDependBean depend1 = new DictCategoryDependBean();
					depend1.setChildId(arrayId[i]);
					depend1.setMasterId(bean.getId());
					GUID guid = new GUID();
					depend1.setId(guid.toString());
					session.save(depend1);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<DictCategoryBean> queryDictCategoryBeanByMasterId(
			String masterId) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("select a from DictCategoryBean a");
		sb.append(" , DictCategoryDependBean b where ");
		sb.append(" a.id=b.childId and  ");
		sb.append(" b.masterId=? ");
		
		return getSession().createQuery(sb.toString()).setString(0, masterId).list();
	}

	@Override
	public void removeDictCategoryById(String id) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("delete c from ");
		sb.append(" sys_dict c ");
		sb.append(" where ");
		sb.append(" c.ID in ('");
		sb.append(id.replaceAll(",", "','"));
		sb.append("') ");
		getSession().createSQLQuery(sb.toString()).executeUpdate();
	}
}
